#include <iostream>
#include <vector>
#include <memory>

class Axid_tank
{
private:
    long int axid_volume;
    bool is_defined;
public:
    Axid_tank(const long int& axid_volume):axid_volume(axid_volume){is_defined = true;}
    Axid_tank():Axid_tank(0){is_defined = false;}
    Axid_tank(const Axid_tank& that)
    {
        is_defined = that.is_defined;
        this->axid_volume = that.axid_volume;
    }
    ~Axid_tank() = default;

    Axid_tank& operator =(const Axid_tank& that)
    {
        this->axid_volume = that.axid_volume;
        return *this;
    }

    long get()
    {
        if (!is_defined) throw std::runtime_error("Undefined value");
        return axid_volume;
    }

    bool operator ==(const Axid_tank& that)
    {
        return (this->axid_volume == that.axid_volume);
    }
    bool operator !=(const Axid_tank& that)
    {
        return !(*this == that);
    }
    bool operator <(const Axid_tank& that)
    {
        return (this->axid_volume < that.axid_volume);
    }
    bool operator >(const Axid_tank& that)
    {
        return (this->axid_volume > that.axid_volume);
    }
    bool operator <=(const Axid_tank& that)
    {
        return !(*this > that);
    }
    bool operator >=(const Axid_tank& that)
    {
        return !(*this < that);
    }

    Axid_tank& operator ++()
    {
        ++axid_volume;
        return *this;
    }
    Axid_tank& operator --()
    {
        --axid_volume;
        return *this;
    }
    Axid_tank operator ++(int)
    {
        Axid_tank prev = *this;
        ++*this;
        return prev;
    }
    Axid_tank operator --(int)
    {
        Axid_tank prev = *this;
        --*this;
        return prev;
    }

    long operator -(const Axid_tank& that)
    {
        return this->axid_volume - that.axid_volume;
    }
    long operator +(const Axid_tank& that)
    {
        return this->axid_volume + that.axid_volume;
    }
};

class Tanks
{
private:
    std::shared_ptr<std::vector<Axid_tank>> tanks;
    void make_container(const std::vector<long>& array, const size_t& size)
    {
        tanks = std::make_shared<std::vector<Axid_tank>>(size);
        std::vector<long> :: const_iterator pointer = array.begin();
        for (size_t i = 0; i < size; ++i)
        {
            tanks->at(i) = *pointer;
            ++pointer;
        }
    }
    long convert_number(const std::string& num)
    {
        return atol(num.c_str());
    }
    std::shared_ptr<std::vector<long>> convert_string(const std::string& str, size_t array_size)
    {
        std::shared_ptr<std::vector<long>> ret = std::make_shared<std::vector<long>>(array_size);
        size_t pos = 0;
        size_t i = 0;
        while (pos < str.size())
        {
            size_t it = str.find(" ", pos);
            if (it == std::string::npos) it = str.size();
            ret->at(i) = convert_number(str.substr(pos, it-pos));
            pos = it + 1;
            ++i;
        }
        return ret;
    }
    std::shared_ptr<std::vector<long>> convert_vector(const std::vector<std::string>& array, size_t array_size)
    {
        std::shared_ptr<std::vector<long>> ret = std::make_shared<std::vector<long>>(array_size);
        size_t i = 0;
        for (auto str : array)
        {
            ret->at(i) = convert_number(str);
            ++i;
        }
        return ret;
    }
public:
    Tanks(const std::string array, const std::string size)
    {
        size_t length = convert_number(size);
        make_container(*convert_string(array, length), length);
    }
    Tanks(const std::vector<std::string> array, const std::string size)
    {
        size_t length = convert_number(size);
        make_container(*convert_vector(array, length), length);
    }
    ~Tanks() = default;

    bool is_non_decraising()
    {
        Axid_tank prev;
        for (auto it : *tanks)
        {
            if (it < prev) return false;
            prev = it;
        }
        return true;
    }

    long substract_last_and_first()
    {
        return tanks->back() - tanks->front();
    }
};

long solve(std::vector<std::string> array, std::string size)
{
    Tanks tank(array, size);
    if (tank.is_non_decraising()) return tank.substract_last_and_first();
    return -1;
}

int main()
{
    std::string size;
    std::cin >> size;

    size_t size_ = atol(size.c_str());
    std::vector<std::string> array(size_);
    for (size_t i = 0; i < size_; ++i) std::cin >> array.at(i);

    //std::cout << size << std::endl;
    //for (auto str : array) std::cout << str << std::endl;

    std::cout << solve(array, size);
    return 0;
}
